package GUIMain;

import java.awt.Window;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.util.Stack;

import javax.swing.JDialog;
import javax.swing.JFrame;

public class GUIFocusManager {
	private static Stack<Window> componentStack;
	private static WindowFocusListener returnFocus;
	private static boolean debug =false;
	private static boolean minimized;
	
	public static void initialize(){
		componentStack = new Stack<Window>();
		//This listener is applied to every window that this class manages
		minimized = false;
		returnFocus = new WindowFocusListener() {
			@Override
			public void windowLostFocus(WindowEvent e) {}
			@Override
			/**
			 * When this window gains focus we check to see if it is
			 * the window on top as dictated by the stack.  If it isn't
			 * we call the topmost window to the front.
			 */
			public void windowGainedFocus(WindowEvent e) {
				if(!e.getComponent().equals(componentStack.peek())){
					componentStack.peek().toFront();
				}
			}
		};
	}
	public static void minimizeAll(){
		minimized = true;
		//Close filechooser if open
		FileChooser.closeFileChooser();
		//hide all windows
		for(int i=0;i<componentStack.size();i++){
			componentStack.get(i).setVisible(false);
		}
	}
	public static void restoreAll(){
		minimized = false;
		//hide all windows
		for(int i=0;i<componentStack.size();i++){
			componentStack.get(i).setVisible(true);
			componentStack.get(i).toFront();
			
			if(componentStack.get(i) instanceof JFrame){
				((JFrame)componentStack.get(i)).setExtendedState(JFrame.NORMAL);
			}
		}
	}
	public static boolean isMinimized(){
		return minimized;
	}
	/**
	 * Adds a window to be managed by the FocusManager and calls focus
	 * to it.
	 * @param c Window to be managed
	 */
	public static void add(Window c){

		if(!componentStack.isEmpty()){
			componentStack.peek().setEnabled(false);
		}
		//If this class does not have an attached window focus listener, we
		//add one to it
		if(c.getListeners(WindowFocusListener.class).length<1){
			c.addWindowFocusListener(returnFocus);
		}
		componentStack.push(c);
		if(debug)
			System.out.println("Current Stack Size (Open) "  + getStackCount());
		if(debug)
			System.out.println("Stack contains: " + componentStack.toString());
		c.setEnabled(true);
		c.setVisible(true);
		c.toFront();
	}
	/**
	 * Removes a window from management and returns control
	 * to the window beneath it
	 */
	public static void remove(){
		if(!componentStack.isEmpty()){
			componentStack.peek().setVisible(false);
			

			componentStack.pop();
			if(debug)
				System.out.println("Current Stack Size (Close) "  + getStackCount());
			componentStack.peek().setEnabled(true);
			componentStack.peek().toFront();
		}
	}
	/**
	 * Debugging purposes
	 * @return number of windows in the stack
	 */
	public static int getStackCount(){
		return componentStack.size();
	}
	public static void setDialogProperties(JDialog window){
		window.pack();
		window.setMinimumSize(window.getSize());
		window.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		window.setLocationRelativeTo(null);
	}
}
